package cn.xyafu.lost.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import cn.xyafu.lost.R;
import cn.xyafu.lost.utils.LostOpenHelper;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
* @author wangdh
* @time 2021/12/7 0007
* desc： 消息Fragment
*/
public class MessageFragment extends BaseFragment {

    private EditText edt_lost_title;
    private EditText edt_lost_msg;
    private Button btn_lost_addbtn;

    private FragmentActivity mContent;

    LostOpenHelper openHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContent = getActivity();
        View view =  inflater.inflate(R.layout.fragment_message, container, false);
        initView(view);

        btn_lost_addbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addLost();
            }
        });

        return view;
    }

    private void initView(View v){
        edt_lost_title = v.findViewById(R.id.edt_lost_title);
        edt_lost_msg = v.findViewById(R.id.edt_lost_msg);
        btn_lost_addbtn = v.findViewById(R.id.btn_lost_addbtn);
    }

    private void addLost(){
        String title = edt_lost_title.getText().toString().trim();
        String msg = edt_lost_msg.getText().toString().trim();
        // 标题和内容为空，不能发布
        if (title == "" || msg == ""){
            Toast.makeText(mContent,"标题和描述不能为空！",Toast.LENGTH_SHORT).show();
            return ;
        }
        String uid = "7";
        openHelper = new LostOpenHelper(getContext(),"lostdb",null,1);
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from t_user", null);
        cursor.moveToFirst();	//移至第一行
        for(int i = 0; i < cursor.getCount(); i++){	//遍历每行数据
            // 获取列数据
            uid =  cursor.getString(6);
        }

        //  发起网络请求
        // (1). 创建OkHttpCLient对象
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5, TimeUnit.SECONDS);
        builder.readTimeout(5, TimeUnit.SECONDS);
        builder.writeTimeout(5, TimeUnit.SECONDS);
        OkHttpClient client = builder.build();
        // (2). 创建Request对象
        Request request = new Request.Builder().url("http://localhost/Lost_ssm/android/addLost?ltitle="+ title +"&lmsg="+ msg +"&uid="+ uid).get().build();
        // (3). 创建Call对象
        Call call = client.newCall(request);
        // (4). 将请求添加到队列中
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功");

                String jsonString = response.body().string();

                if (jsonString.contains("true")){
                    showToast("发布成功!",true);
                }else{
                    showToast("网络异常，请稍后重试！",false);
                }

            }
        });


    }

    private void showToast(final String toast, final Boolean flag){

        btn_lost_addbtn.post(new Runnable() {
            @Override
            public void run() {
                if (flag){
                    edt_lost_msg.setText("");
                    edt_lost_title.setText("");
                }
                Toast.makeText(mContent,toast,Toast.LENGTH_SHORT).show();
            }
        });

    }






}